<?php

function ajax_login() {
    $creds = array();
    $creds['user_login'] = $_POST['email'];
    $creds['user_password'] = $_POST['password'];
    //$creds['remember'] = false;
    $user = wp_signon($creds, false);

    if (is_wp_error($user)) {
        return array('sts' => 'error', 'message' => __('login failed. please try again'));
    } else {
        return array('sts' => 'success', 'url' => get_permalink_ajax(33));
    }
}

function ajax_register() {

    
    require_once('recaptchalib.php');
    $privatekey = "6LfPgesSAAAAANZHufADA7VJ4oGVF5c1MI5elw9O ";
    $resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);

    if (!$resp->is_valid) {
        return array('sts' => 'error', 'message' => __('Invalid Captcha'));
    }


    $data = array();
    $data['first_name'] = $_POST['firstname'];
    $data['last_name'] = $_POST['lastname'];
    $data['user_login'] = $_POST['email'];
    $data['user_pass'] = $_POST['password'];
    $data['user_email'] = $_POST['email'];

    //addition data
    $add_data = array();
    $add_data['pass'] = $_POST['password'];
    $add_data['address'] = $_POST['address'];
    $add_data['apt'] = $_POST['apt'];
    $add_data['city'] = $_POST['city'];
    $add_data['phone'] = $_POST['phone'];
    $add_data['age'] = $_POST['age'];
    $add_data['gender'] = $_POST['gender'];
    $add_data['i-have-read'] = !empty($_POST['i-have-read']) ? $_POST['i-have-read'] : 'off';
    $add_data['email-update'] = !empty($_POST['email-update']) ? $_POST['email-update'] : 'off';


    //check email
    if (!is_email($data['user_login'])) {
        return array('sts' => 'error', 'message' => __('login failed. invalid email'));
    }

    //check exist
    if (username_exists($data['user_login'])) {
        return array('sts' => 'error', 'message' => __('Sorry, that email address is already used!'));
    }

    // add user
    $user = wp_insert_user($data);

    if (is_wp_error($user)) {
        return array('sts' => 'error', 'message' => $user->get_error_message());
    } else {
        
        // add post meta
        foreach ($add_data as $key => $value) {
            add_user_meta($user, $key, $value, false);
        }

        //addpoint for invite user
        // 1 get invite user
        /*
        if (!empty($_SESSION['invite'])) {
            $where_uid = "MD5( CONCAT( '" . SALT . "', id ) ) = '" . $_SESSION['invite'] . "'";
            $qr = "select id from wp_users where " . $where_uid;
            $uid = find($qr, 'var');

            if ($uid) {
                // check invite 5ng this week
                if (check_invite_5($uid)) {
                    addPoint($uid, INVITE_TYPE, $data['user_login']);
                    unset($_SESSION['invite']);
                    $user_info = get_userdata($uid);
                    $arr_val = array(
                        'login_url' => get_permalink_ajax(6),
                    );
                    $kq = ph_mail($user_info->user_email, t('friend_mail_sub', false), $arr_val, 'friend');
                }
            }
        }
         */

        //login
        $creds = array();
        $creds['user_login'] = $data['user_login'];
        $creds['user_password'] = $data['user_pass'];
        $creds['remember'] = true;
        $user = wp_signon($creds, false);
        if (is_wp_error($user)) {
            echo $user->get_error_message();
        }

        //send mail register
        //$kq = ph_mail( $data['user_email'], t( 'res_mail_sub' ,false), null , 'register');
        // check con qua ko?
        /*
          $qr = "select * from feliciations";
          $redirect = get_permalink_ajax(69);

          if(count(find($qr))<500)
          {
          $redirect = get_permalink_ajax(52);
          }
         */
        $redirect = get_permalink_ajax(33);
        return array('sts' => 'success', "user_id" => $user, 'message' => __('registration success'), 'redirect' => $redirect);
    }
}

function get_permalink_ajax($id) {
    return qtrans_convertURL(get_permalink($id), null, true);
    //return get_permalink($id);
}
